package suanfa.lianxi;

import java.util.EmptyStackException;

public class Cart<T> {
        public product[] arr;
        private int capacity=10;//指向栈底的指针
        private int top=-1;//指向栈顶的指针，初始值：-1
        private int size;

        public Cart(int capacity){
            arr=(product[])new product[capacity];
            size=0;
        }
        public void resize(int n){
            if(n< arr.length){
                return;
            }
            product[] old=arr;
            arr=(product[])new Object[n];
            for(int i=0;i<size;i++){
                arr[i]=old[i];
            }
        }
    public void push(product data){
        if(size==arr.length){
            resize(arr.length*2+1);
        }
        arr[++top]=data;
        size++;
    }
    public boolean isEmpty(){
            return  size==0;
    }
    public product pop() throws EmptyStackException {
        if(isEmpty()){
            throw new EmptyStackException();
        }
        size--;
        return arr[top--];
    }

    public static void main(String[] args) {
        Cart<product> cart=new Cart<>(3);
        product p1=new product(1,"娃哈哈矿泉水",2,4);
        product p2=new product(2,"乐事薯片",5,5);
        cart.push(p1);
        cart.push(p2);

        int sumprice=0;
        int sumq=1;
        int sum=0;
        for(int i=0;i<cart.size;i++){

            sumprice=cart.arr[i].price;//取价格
            sumq=cart.arr[i].quantity;//取数量
            sum+=sumprice*sumq;

        }
        System.out.println("总价为: "+sum);



    }

    }
 class product{//物品类
    public int id;
    public String productName;
    public int price;
    public int quantity;

    public product(int i,String pro,int p,int q){
        this.id=i;
        this.productName=pro;
        this.price=p;
        this.quantity=p;

    }
}

